package com.valiscalc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.valiscalc.entity.SuperLotto;
import com.valiscalc.service.SuperLottoService;
import com.valiscalc.util.response.Result;
import com.valiscalc.util.statistics.LottoStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 大乐透实票
 *
 * 1.尽量避免大量查询数据库
 * 2.尽量将数据放在缓存中
 * 3.尽量提供实时数据
 *
 * </p>
 *
 * @author vallis
 * @since 2020-11-17
 */
@RestController
@RequestMapping("/super-lotto")
public class SuperLottoController {
    @Autowired
    private SuperLottoService superLottoService;
    
    //缓存 --》供实时查询
    public static  List<SuperLotto> lottoTickets=new ArrayList<>(200);
    
    
    
    /**
     * 录入彩票
     * @param
     * @return
     */
    @RequestMapping("addLottoTicket")
    public Result addLottoTicket(){
       
    
       
       
       //将此票拆解并录入统计库中
        System.out.println(1);
        SuperLotto superLotto=new SuperLotto();
        superLotto.setFrontNum("1,2,3,4,5,6,7");
        superLotto.setEndNum("2,3,4,5");
     
    
        LottoStatistics lottoStatistics=new LottoStatistics();
    
        //校验这张彩票 是否符合标准---》  符合：true  不符合：false
        boolean correct = lottoStatistics.isCorrect(superLotto);
        //金额检测不合格-》拒绝输入
        if (!correct){
            return new Result(true,null,null);
        }
        
        //校验这张彩票 是否在库中重复---> 重复：true 不重复：false
        boolean react = isReact(superLotto);
        //数据库检测到重复-》拒绝输入
        if (react){
            return new Result(true,null,null);
        }
        //计算他的花费金额
        long cost=lottoStatistics.getSinglePrice(superLotto);
        //完成检验--》录入数据库
        superLottoService.save(superLotto);
        
        //加入到缓存库
        lottoTickets.add(superLotto);
        //将此票分解成各个统计结果-存入数据库
        //将此票的统计结果-》存入缓存库
        
    
        
    
        
        
        
        
    
        return new Result(true,null,null);
    }
    
    /**
     * 判断数据库中是否有重复票
     * @param superLotto
     * @return
     */
    public  boolean isReact(SuperLotto superLotto){
        List<SuperLotto> one =  superLottoService.list(new QueryWrapper<SuperLotto>().eq("front_num", superLotto.getFrontNum()).eq("end_num", superLotto.getEndNum()));
        if (one.size()>0){
            System.out.println("存在"+one.size());
            return true;
        }
        System.out.println("不存在"+one.size());
        return false;
    }
    /**
     * 查询
     * @return
     */
    public Result LottoTicket(){
    
        return new Result(true,null,null);
    }
}
